/*
 * TwittView.java
 *
 * @version 0.0.1.14581
 * @author Sad_hamster
 */

package twitt;

import TwitterClient.TwitterWrap;
import java.awt.Color;
import java.awt.Component;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Vector;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
//
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import twitter4j.Twitter;
import twitter4j.TwitterException;


/**
 * The application's main frame.
 */
public class TwittView extends FrameView {

    static Twitter Twit;
    static String lastState;
   // DefaultListModel istModel = new DefaultListModel();
    static Vector personalTwits = new Vector();
    static Vector conntactNames = new Vector();
    private Vector ColumnName = new Vector(3);
    private DefaultTableModel twittsInList  = null;
    private NewGridRender myRender = new NewGridRender();

    public TwittView(SingleFrameApplication app) {
        super(app);

        
        ColumnName.add("Picture");
        ColumnName.add("Time/Name");
        ColumnName.add("Message");
        twittsInList = new DefaultTableModel(personalTwits, ColumnName);
        //twittsInList.setDataVector
        initComponents();
        
        //jTable1.setDefaultRenderer(Vector.class, new NewGridRender());


        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
        lastState = "";
//        listContacts.addMouseListener(this);
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = TwittApp.getApplication().getMainFrame();
            aboutBox = new TwittAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        TwittApp.getApplication().show(aboutBox);
    }

    @Action
  public void SayMessage() {

      JFrame mainFrame = TwittApp.getApplication().getMainFrame();
      String sayForWho = "";
      if (cbServisType.getSelectedIndex() != 0) {
        sayForWho = listContacts.getSelectedValue().toString();
      }
      SayMessageBox sayBox = new SayMessageBox(mainFrame, true, sayForWho);
      sayBox.setLocationRelativeTo(mainFrame);
      TwittApp.getApplication().show(sayBox);
    try {
      TwitterWrap tw = new TwitterWrap();
      sayForWho =  sayBox.GetMessageToSend().SendForMan;
      // TODO : научиться слать приватные сообщения
      // TODO Сделать сообщение что текст отправлен
      JOptionPane.showMessageDialog(mainFrame, tw.SendMessage(Twit, sayForWho, sayBox.GetMessageToSend().SendingMessage, sayBox.GetMessageToSend().SendAsPrivate));
    } catch (Exception ex) {
      Logger.getLogger(TwittView.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  // TODO : Научиться переходить на сообщения автора, или сообщения того кому был передан ответ
  // переходим на первую попавшуюся ссылку

  // TODO : Научиться удалять сообщения
  // TODO : научться удалять приватные сообщения

    @Action
  public void ShowPersonMessage() {
      JFrame mainFrame = TwittApp.getApplication().getMainFrame();

      String twittOwner = "";
      String message = "";
      TwitterWrap tw = new TwitterWrap();
      if ((cbServisType.getSelectedIndex() == 0) || (cbServisType.getSelectedIndex() == 3)) {
              twittOwner = tw.GetLogin();
      } else {
        if (listContacts.getSelectedIndex() == -1){
          try 
            {twittOwner = conntactNames.get(0).toString();}
          catch (Exception e)
            {twittOwner = "";}
        } else {twittOwner = listContacts.getSelectedValue().toString();}
      }
      if (jTable1.getSelectedRow() == -1) {
        try
          {message = ((Vector)personalTwits.get(0)).get(2).toString();}
        catch (Exception e)
          {message = "";}
      } else {
        message = ((Vector)personalTwits.get(jTable1.getSelectedRow())).get(2).toString();
      }
      TwitterData listenBox = new TwitterData(mainFrame, true, message ,twittOwner);
      listenBox.setLocationRelativeTo(mainFrame);

      TwittApp.getApplication().show(listenBox);
      personName.setText(twittOwner);
  }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
   @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        spContacts = new javax.swing.JScrollPane();
        listContacts = new javax.swing.JList(conntactNames);
        cbServisType = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable(){
            public TableCellRenderer getCellRenderer(int row, int column) {
                if (column == 0) {
                    return myRender;
                } else {
                    if (column == 1) {this.getTableHeader().getColumnModel().getColumn(column).setWidth(150);
                        this.getColumnModel().getColumn(column).setWidth(150);
                    }
                    return super.getCellRenderer(row, column);
                }
            }
        };
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuTwitterLogin = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        messageMenu = new javax.swing.JMenu();
        sayMessageMenuItem = new javax.swing.JMenuItem();
        showMessageMenuItem = new javax.swing.JMenuItem();
        showAuthorMessageMeunItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        jStatusLabel = new javax.swing.JLabel();
        personName = new javax.swing.JLabel();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                mainPanelComponentResized(evt);
            }
        });

        spContacts.setName("spContacts"); // NOI18N

        listContacts.setModel(new javax.swing.AbstractListModel() {
            public int getSize() { return conntactNames.size(); }
            public Object getElementAt(int i) { return conntactNames.get(i); }
        });
        listContacts.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listContacts.setName("listContacts"); // NOI18N
        listContacts.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listContactsMouseClicked(evt);
            }
        });
        spContacts.setViewportView(listContacts);

        cbServisType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Мои", "Кто следует", "За кем следую", "Главная" }));
        cbServisType.setName("cbServisType"); // NOI18N
        cbServisType.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbServisTypeItemStateChanged(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(twittsInList);
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cbServisType, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(spContacts, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addComponent(spContacts, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbServisType, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11))
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(twitt.TwittApp.class).getContext().getResourceMap(TwittView.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(twitt.TwittApp.class).getContext().getActionMap(TwittView.class, this);
        jMenuTwitterLogin.setAction(actionMap.get("ShowLoginPage")); // NOI18N
        jMenuTwitterLogin.setText(resourceMap.getString("jMenuTwitterLogin.text")); // NOI18N
        jMenuTwitterLogin.setActionCommand(resourceMap.getString("jMenuTwitterLogin.actionCommand")); // NOI18N
        jMenuTwitterLogin.setName("jMenuTwitterLogin"); // NOI18N
        fileMenu.add(jMenuTwitterLogin);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        messageMenu.setAction(actionMap.get("ifMenuMessageSelect")); // NOI18N
        messageMenu.setText(resourceMap.getString("messageMenu.text")); // NOI18N
        messageMenu.setActionCommand(resourceMap.getString("messageMenu.actionCommand")); // NOI18N
        messageMenu.setName("messageMenu"); // NOI18N
        messageMenu.addMenuListener(new javax.swing.event.MenuListener() {
            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
            public void menuDeselected(javax.swing.event.MenuEvent evt) {
            }
            public void menuSelected(javax.swing.event.MenuEvent evt) {
                messageMenuMenuSelected(evt);
            }
        });

        sayMessageMenuItem.setAction(actionMap.get("SayMessage")); // NOI18N
        sayMessageMenuItem.setText(resourceMap.getString("sayMessageMenuItem.text")); // NOI18N
        sayMessageMenuItem.setActionCommand(resourceMap.getString("sayMessageMenuItem.actionCommand")); // NOI18N
        sayMessageMenuItem.setName("sayMessageMenuItem"); // NOI18N
        messageMenu.add(sayMessageMenuItem);
        sayMessageMenuItem.getAccessibleContext().setAccessibleName(resourceMap.getString("jMenuItem3.AccessibleContext.accessibleName")); // NOI18N

        showMessageMenuItem.setAction(actionMap.get("ShowPersonMessage")); // NOI18N
        showMessageMenuItem.setText(resourceMap.getString("showMessageMenuItem.text")); // NOI18N
        showMessageMenuItem.setActionCommand(resourceMap.getString("showMessageMenuItem.actionCommand")); // NOI18N
        showMessageMenuItem.setName("showMessageMenuItem"); // NOI18N
        messageMenu.add(showMessageMenuItem);
        showMessageMenuItem.getAccessibleContext().setAccessibleName(resourceMap.getString("jMenuItem2.AccessibleContext.accessibleName")); // NOI18N

        showAuthorMessageMeunItem.setAction(actionMap.get("ShowAuthorMessage")); // NOI18N
        showAuthorMessageMeunItem.setText(resourceMap.getString("showAuthorMessageMeunItem.text")); // NOI18N
        showAuthorMessageMeunItem.setActionCommand(resourceMap.getString("showAuthorMessageMeunItem.actionCommand")); // NOI18N
        showAuthorMessageMeunItem.setName("showAuthorMessageMeunItem"); // NOI18N
        messageMenu.add(showAuthorMessageMeunItem);
        showAuthorMessageMeunItem.getAccessibleContext().setAccessibleName(resourceMap.getString("jMenuItem4.AccessibleContext.accessibleName")); // NOI18N

        menuBar.add(messageMenu);
        messageMenu.getAccessibleContext().setAccessibleName(resourceMap.getString("jMenu1.AccessibleContext.accessibleName")); // NOI18N

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        jStatusLabel.setFont(resourceMap.getFont("jStatusLabel.font")); // NOI18N
        jStatusLabel.setForeground(resourceMap.getColor("jStatusLabel.foreground")); // NOI18N
        jStatusLabel.setText(resourceMap.getString("jStatusLabel.text")); // NOI18N
        jStatusLabel.setName("jStatusLabel"); // NOI18N

        personName.setText(resourceMap.getString("personName.text")); // NOI18N
        personName.setName("personName"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 646, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(statusMessageLabel)
                    .addComponent(jStatusLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 444, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
            .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(statusPanelLayout.createSequentialGroup()
                    .addGap(0, 323, Short.MAX_VALUE)
                    .addComponent(personName)
                    .addGap(0, 323, Short.MAX_VALUE)))
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                        .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(statusMessageLabel)
                            .addComponent(statusAnimationLabel)
                            .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(3, 3, 3))
                    .addGroup(statusPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jStatusLabel)
                        .addContainerGap())))
            .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(statusPanelLayout.createSequentialGroup()
                    .addGap(0, 16, Short.MAX_VALUE)
                    .addComponent(personName)
                    .addGap(0, 17, Short.MAX_VALUE)))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private class NewGridRender extends JLabel implements TableCellRenderer {

        public NewGridRender() { super(); }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (column == 0) {
                ImageIcon ico = new ImageIcon((java.net.URL)value);
                table.setRowHeight(row, ico.getIconHeight());
                table.getTableHeader().getColumnModel().getColumn(column).setWidth(ico.getIconWidth());
     //           table.getColumnModel().getColumn(column).setWidth(ico.getIconWidth());
                setIcon(ico);
            }
            return this;
        }
    }

    private void mainPanelComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_mainPanelComponentResized
      /*
       * Изменяем размеры списков в соответствии с размерами основной панели.
      */
    }//GEN-LAST:event_mainPanelComponentResized

    private void listContactsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listContactsMouseClicked
      String personNameText = "";
      String personIndex = "";
      if (Twit != null) {
        TwitterWrap tw = new TwitterWrap();
      try {
        personNameText = listContacts.getSelectedValue().toString().split("; ")[0];
        personIndex = listContacts.getSelectedValue().toString().split("; ")[1];
        personalTwits = tw.GetPersonsTimeline(Twit, personIndex);
        twittsInList.setDataVector(personalTwits, ColumnName);
      } catch (TwitterException ex) {
        Logger.getLogger(TwittView.class.getName()).log(Level.SEVERE, null, ex);
      }
        twittsInList.setDataVector(personalTwits, ColumnName);
      }
      this.personName.setText(personNameText);
    }//GEN-LAST:event_listContactsMouseClicked

    private void cbServisTypeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbServisTypeItemStateChanged
      if (!lastState.equals(cbServisType.getSelectedItem().toString())) {
        TwitterWrap tw = new TwitterWrap();
        lastState = cbServisType.getSelectedItem().toString();
        switch (cbServisType.getSelectedIndex()){
          case 0: { try {

            personalTwits = tw.GetPersonsTimeline(Twit, Twit.getUserId());
            conntactNames.clear();
            conntactNames.add("!!!! За кем следую !!!!");
            conntactNames.addAll(tw.GetFollowersPersons(Twit, false));
            conntactNames.add("!!!! Кто следует !!!!");
            conntactNames.addAll(tw.GetFollowersPersons(Twit, true));
            } catch (TwitterException ex) {
              Logger.getLogger(TwittView.class.getName()).log(Level.SEVERE, null, ex);
            }
            break;
          }
          case 1: { try {
            conntactNames = tw.GetFollowersPersons(Twit, true);
            personalTwits = tw.GetPersonsTimeline(Twit, conntactNames.get(0).toString().split("; ")[1]);
            } catch (TwitterException ex) {
              Logger.getLogger(TwittView.class.getName()).log(Level.SEVERE, null, ex);
            }
            break;
          }
          case 2: { try {
            conntactNames = tw.GetFollowersPersons(Twit, false);
            personalTwits = tw.GetPersonsTimeline(Twit, conntactNames.get(0).toString().split("; ")[1]);
            } catch (TwitterException ex) {
              Logger.getLogger(TwittView.class.getName()).log(Level.SEVERE, null, ex);
            }
            break;
          }
          case 3: { try {
            conntactNames.clear();
            personalTwits = tw.GetPersonsTimeline(Twit);
            } catch (TwitterException ex) {
              Logger.getLogger(TwittView.class.getName()).log(Level.SEVERE, null, ex);
            }
            break;
          }
          
        }
        twittsInList.setDataVector(personalTwits, ColumnName);
        listContacts.setListData(conntactNames);
      }

    }//GEN-LAST:event_cbServisTypeItemStateChanged

    private void messageMenuMenuSelected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_messageMenuMenuSelected
      int personIndex = jTable1.getSelectedRow();
      int twittIndex = jTable1.getSelectedRow();

      boolean isVisible = (!(personIndex == -1) || !(twittIndex == -1));
      sayMessageMenuItem.setVisible((isVisible) || (jStatusLabel.getText().equals("OnLine")));
      showMessageMenuItem.setVisible(isVisible);
      showAuthorMessageMeunItem.setVisible(isVisible && (((Vector)personalTwits.get(jTable1.getSelectedRow())).get(2).toString().split("@").length > 1));
    }//GEN-LAST:event_messageMenuMenuSelected

  @Action
  public void ShowLoginPage() {
    JFrame mainFrame = TwittApp.getApplication().getMainFrame();
    NewLogin dl = new NewLogin(mainFrame, true);
    dl.setLocationRelativeTo(TwittApp.getApplication().getMainFrame());
    if (dl.isVisible()){
      dl.setFocusable(true);
    } else {
      dl.setVisible(true);
    }
    try {
      ChckLogindata();
    } catch (Exception ex) {
      Logger.getLogger(TwittView.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  @Action
  public void ChckLogindata() throws Exception {

     TwitterWrap tw = new TwitterWrap();
     if (tw.GetConnectionStatus() <= 0){
       if (tw.GetLogin().equals("")) {return;}
       Twit = (Twitter) tw.DoConect(Twit);
     }
     switch (tw.GetConnectionStatus()) {
       case 0: Twit = (Twitter) tw.DoConect(Twit);
       case -1: {
           javax.swing.JOptionPane.showMessageDialog(mainPanel, "Error on connection");
           throw new Exception("Error on connection");
       }
     }

    //////////////////////////////////////
    if (tw.GetConnectionStatus() != -1) {
      jStatusLabel.setText("OnLine");
      jStatusLabel.setForeground(Color.green);
      cbServisTypeItemStateChanged(null);
    }
  }

  void ChangePopupMenu(int caseOfMenu){
  }

  @Action
  public void ShowAuthorMessage() {
    if (jTable1.getSelectedRow() == -1) {return;}

    String message = ((Vector)personalTwits.get(jTable1.getSelectedRow())).get(2).toString();
    String name = message.split("@")[1].split(" ")[0];
    if (name.equals("")) {return;}

     if (Twit != null) {
        TwitterWrap tw = new TwitterWrap();
      try {
        personalTwits = tw.GetPersonsTimeline(Twit, name);
      } catch (TwitterException ex) {
        Logger.getLogger(TwittView.class.getName()).log(Level.SEVERE, null, ex);
      }
        twittsInList.setDataVector(personalTwits, ColumnName);
      }
    personName.setText(name);
  }

 


  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbServisType;
    private javax.swing.JMenuItem jMenuTwitterLogin;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jStatusLabel;
    private javax.swing.JTable jTable1;
    private javax.swing.JList listContacts;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu messageMenu;
    private javax.swing.JLabel personName;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JMenuItem sayMessageMenuItem;
    private javax.swing.JMenuItem showAuthorMessageMeunItem;
    private javax.swing.JMenuItem showMessageMenuItem;
    private javax.swing.JScrollPane spContacts;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
